package com.lance.answer.util.image_valid;

import com.lance.answer.util.HttpUtil;
import com.lance.answer.util.StringUtils;
import net.sourceforge.tess4j.ITessAPI;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * OEM_TESSERACT_ONLY,           // Run Tesseract only - fastest                                             很快
 * OEM_CUBE_ONLY,                // Run Cube only - better accuracy, but slower                           准确但是慢
 * OEM_TESSERACT_CUBE_COMBINED,  // Run both and combine results - best accuracy     准确
 * OEM_DEFAULT                   // Specify this mode when calling init_*(),
 */
public class ValidUtils {

    static String DOMAIN = "https://iam.pt.ouchn.cn";

    public static String read(String imageFilePath) throws TesseractException, IOException {
        ITesseract instance = new Tesseract();
        instance.setDatapath("libs/tessdata");
        instance.setLanguage("eng");
        instance.setOcrEngineMode(ITessAPI.TessOcrEngineMode.OEM_TESSERACT_LSTM_COMBINED);
        //instance.setTessVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
        instance.setTessVariable("tessedit_char_whitelist", "1234567890");
        File imageFile = new File(imageFilePath);
        //File dstFile = ImageUtil.cleanImage(imageFile,"C:\\Users\\Administrator\\Desktop\\验证码2\\deal\\");
        String text = instance.doOCR(imageFile);
        if (StringUtils.isNotEmpty(text)) {
            text = text.replaceAll(" ","");
            text = text.replaceAll(" ","");
            text = text.replaceAll("-","");
        }
       return text;
    }

    public static String valid(String url) throws TesseractException, IOException {
        url = DOMAIN + url;
        ITesseract instance = new Tesseract();
        instance.setDatapath("libs/tessdata");
        instance.setLanguage("eng");
        instance.setOcrEngineMode(ITessAPI.TessOcrEngineMode.OEM_TESSERACT_LSTM_COMBINED);
        //instance.setTessVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
        instance.setTessVariable("tessedit_char_whitelist", "1234567890");
        BufferedImage bufferedImage = HttpUtil.downloadImage(url);
        //File dstFile = ImageUtil.cleanImage(imageFile,"C:\\Users\\Administrator\\Desktop\\验证码2\\deal\\");
        String text = instance.doOCR(bufferedImage);
        if (StringUtils.isNotEmpty(text)) {
            text = text.replaceAll(" ","");
            text = text.replaceAll(" ","");
            text = text.replaceAll("-","");
        }
        return text;
    }

    public static String valid(File file) throws TesseractException, IOException {
        ITesseract instance = new Tesseract();
        instance.setDatapath("libs/tessdata");
        instance.setLanguage("eng");
        instance.setOcrEngineMode(ITessAPI.TessOcrEngineMode.OEM_TESSERACT_LSTM_COMBINED);
        //instance.setTessVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
        instance.setTessVariable("tessedit_char_whitelist", "1234567890");
        //File dstFile = ImageUtil.cleanImage(imageFile,"C:\\Users\\Administrator\\Desktop\\验证码2\\deal\\");
        String text = instance.doOCR(file);
        if (StringUtils.isNotEmpty(text)) {
            text = text.replaceAll(" ","");
            text = text.replaceAll(" ","");
            text = text.replaceAll("-","");
        }
        return text;
    }

    public static void main(String[] args) throws TesseractException, IOException {
        String url = "/am/validate.code?id=0.08567676055958273";
       // System.out.println(valid(url));
        System.out.println(read("D:\\ideaworkplace\\answer-tool\\src\\main\\resources\\code_demo\\code1.jpeg"));
    }
}
